<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
Class POPASP_FILE
	Private fso
	
	Private Sub Class_Initialize
		'解析配置文件		
		set fso = Server.CreateObject("Scripting.FileSystemObject")		
	End Sub	
	
	Private Sub Class_Terminate		
		set fso = nothing
	End Sub	
	
	'返回路径中的文件名部分
	'如果参数为数组，则第一个参数对应路径，第二个参数为一boolean值，为真时返回不含后缀的文件名，为假为含后缀
	Function baseName( byref arg )
		dim bool,suffix,filePath
		bool = false

		'处理参数
		if isArray( arg ) Then
			filePath = arg(0)
			if ubound(arg) > 0 then '含有两个参数
				bool = Not is_empty( arg(1) )
			end if
		else
			filePath = arg
		end if
	
		filePath = replace( filePath,"\","/" )	'将路径中的\，用/来替换
		
		
		if POP_MVC.String.Exists( filePath, "/" ) then	'如果口径中含有/
			filePath = POP_MVC.String.rstr( filePath ,"/" )	'找最后一个/
			filePath = mid(filePath,2)
		end if
	
		baseName = filePath
		
		if bool then	'为真时，不含后缀
			if POP_MVC.String.Exists( filePath, "." ) then	'判断文件名中是否含有点
				suffix = POP_MVC.String.rstr( filePath ,"." )	
			end if

			if suffix <> "" Then
				baseName = mid(baseName,1,len(baseName) - len(suffix) )
			end if
		end if
	End Function
	
	'返回路径中的文件名的后缀，默认含.
	'如果参数为数组，则第一个参数对应路径，第二个参数为一boolean值，为真时返回不含点的文件名，为假为含点
	Function extName( ByVal arg )	
		dim bool,filePath
		
		extName = ""
		bool = false
		
		'处理参数
		if isArray( arg ) Then
			filePath = arg(0)
			if ubound(arg) > 0 then '含有两个参数
				bool = (Not is_empty( arg(1) ))
			end if
		else
			filePath = arg
		end if
	
		filePath = baseName( filePath )
		if POP_MVC.String.Exists( filePath, "." ) then	'判断文件名中是否含有点
			extName = POP_MVC.String.rstr( filePath ,"." )	
		end if

		if bool then	'为值时不含点
			extName = mid( extName,2 )
		end if
	End Function
	
	' 从类文件中取得内容，并且删除第一个与最后一个出现在asp代码标记
	Function class_get_contents ( file )
		dim str,start_,end_
		str		= file_get_contents(file)
		start_	= InStr(str,"<" & "%")
		end_	= InStrRev( str,"%" & ">" )
		str= mid(str,start_ + 2,end_-3) 
		asp_get_contents = str
	End Function	
	
	'获取文件内容
	Function file_get_contents( ByVal file_path )		
		file_get_contents = POP_MVC.file_get_contents(file_path)
	End Function
	
	
	' 检测提供的相对路径文件是否为文件
	Function isFile( relativePath )
		isFile = fso.FileExists( POP_MVC.realPath(relativePath) )
	End Function
	
	'检测提供的相对路径文件夹是否存在
	Public Function isFolder(relativePath)
		isFolder = fso.FolderExists(POP_MVC.realPath(relativePath))
	End Function
	
	'检测相对路径的文件或文件夹是否存在
	Public Function isExists(ByVal path)
		isExists = isFile(path) or isFolder(path)
	End Function
	
	'删除文件或文件夹
	Public Sub remove( relativePath )
		if isFile( relativePath ) then
			call fso.deleteFile(POP_MVC.realPath(relativePath),true)
		elseif isFolder( relativePath ) then
			call fso.deleteFolder(POP_MVC.realPath(relativePath),true)
		end if
	End Sub
	
	' 获取某个文件的修改时间
	Function fileSize ( filename )
		dim file 
		If Instr(filename,">")>0 Then 
			set file = fso.GetFile(filename)
		else
			set file = fso.GetFile(POP_MVC.realPath(filename))
		end if	
		fileSize = file.size
		set file = nothing
	End Function

	' 获取某个文件的修改时间
	Function mtime ( filename )
		dim file 
		If Instr(filename,">")>0 Then 
			set file = fso.GetFile(filename)
		else
			set file = fso.GetFile(POP_MVC.realPath(filename))
		end if	
		mtime = file.DateLastModified
		set file = nothing
	End Function	
End Class
%>